<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');
$this->load->view('header');
?>
<section class="container_12 clearfix" id="main">
	<section class="grid_7" id="container">
        <div id="weibo_get_new_msg">
            <div id="weibo_loading"> 
                <div id="weibo_loading_con" class="overlay"></div>
            </div>
            <a id="weibo_get_new_msg_button" href="#" title="Get new message">
                Get new message <span id="weibo_sync_msg">Message : <?php echo $total_rows; ?> </span> 
            </a>
        </div>
        
        <ol class="weibo_list">
            <?php foreach ($weibo_message as $v): ?>
                <li id="<?php echo $v->mid ?>">
                    <?php if (isset($v->retweeted_status)) : ?>
                        
                        
                        <div class="grid_1">
                            <img src="<?php echo $v->user->profile_image_url; ?>">
                        </div>
                        <div class="prefix_1">
                            <p><strong><?php echo $v->user->screen_name ?></strong></p>
                            <p><?php echo $v->text ?></p>
                            <p class="weibo_item_created"><?php echo $this->date->format($v->created_at); ?></p>
                        </div>
                        
                        <div class="triangle"></div>
                        <blockquote>
                            <div class="grid_1">
                                <img src="<?php echo $v->retweeted_status->user->profile_image_url; ?>">
                            </div>
                            <div class="prefix_1">
                                <p><strong><?php echo $v->retweeted_status->user->screen_name ?></strong></p>
                                <p><?php echo $v->retweeted_status->text ?></p>
                                <?php if (isset($v->retweeted_status->thumbnail_pic)): ?>
                                    <figure><img src="<?php echo $v->retweeted_status->thumbnail_pic ?>" alt="转发图片"/></figure>
                                <?php endif; ?>
                            </div>
                        </blockquote>
                    <?php else: ?>
                        <blockquote>
                            <div class="grid_1">
                                <img src="<?php echo $v->user->profile_image_url; ?>">
                            </div>
                            <div class="prefix_1">
                                <p><strong><?php echo $v->user->screen_name ?></strong></p>
                                <p><?php echo $v->text ?></p>
                                <?php if (isset($v->thumbnail_pic)): ?>
                                    <figure><img src="<?php echo $v->thumbnail_pic ?>" alt="转发图片"/></figure>
                                <?php endif; ?>
                                <p class="weibo_item_created"><?php echo $this->date->format($v->created_at); ?></p>
                            </div>
                        </blockquote>
                    <?php endif; ?>    
                </li>
            <?php endforeach; ?>
        </ol>
		
        <div class="pages"> <?php echo $this->pagination->create_links(); ?></div>
	</section>
	
    <aside class="grid_5" id="weibo_sidebar">
        <div id="follow">
            <div id="follower" class="follow">
                <h2 title="follower"> Follower</h2>
                <ul>
                <?php foreach ($followers as $v) : ?>
                    <li><img src="<?php echo $v['profile_image_url']; ?>"></li>
                <?php endforeach ; ?>
                </ul>
            </div>
            <div id="following" class="follow">
                <h2 title=""> Following</h2>
                <ul>
                <?php foreach ($following as $v) : ?>
                    <li><img src="<?php echo $v['profile_image_url']; ?>"></li>
                <?php endforeach ; ?>
                </ul>
            </div>
        </div>

        <div id="weibo_items">
            <div id="weibo_itme_close"> <a href="#">close</a> </div>
            <div id="weibo_item"></div>
        </div>

    </aside>
    
</section>

<div id="msg_box" class="overlay">aaaa</div>

<script type="text/javascript">
    
    var msg_box;
    var weibo_id  = '';
    var current_weibo ;
    var current_weibo_index = -1;
    var weibo_list_li;
    var key_g_click_time = 0;
    
    /**
     * 快捷键
     * @param {Int} evt
     */
    document.onkeypress = function (evt)
    {
        var obj_event = evt?evt:event;
        var key_code = obj_event.keyCode ? obj_event.keyCode : obj_event.which;
        key_code = key_code.toString();
        //alert(key_code);
        
        switch (key_code){
            //key:G, end
            case '71':
                $("html").animate({ scrollTop: $(document).height() }, "slow");
                current_weibo_index = weibo_list_li.length - 1;
                weibo_focus();
                break;
                
            //key:g, home
            case '103' :
                key_g_click_time ++;
                if(key_g_click_time >= 2)
                {
                   $("html").animate({ scrollTop: 0}, "slow");
                    current_weibo_index = 0
                    key_g_click_time = 0;
                    weibo_focus();
                }
                break;
                
            //key:j, action:pre message    
            case '106' :
                current_weibo_index ++;
                if(current_weibo_index >= weibo_list_li.length)
                {
                    current_weibo_index = weibo_list_li.length - 1;
                }
                
                weibo_scroll();
                break;
                
            //key:k, action:next message    
            case '107':
                current_weibo_index --;
                if(current_weibo_index < 0)
                {
                    current_weibo_index = 0;
                }
                
                weibo_scroll();
                break;
                
            //key:r, get new message
            case '114':
                $('#weibo_loading_con').html(get_loading_white_img()).fadeIn('slow');
                $('#weibo_get_new_msg_button').trigger("click");
                break;
            
            //key:v, action:view message    
            case '118':
                if(current_weibo_index < 0)
                {
                    current_weibo_index = 0;
                }
                weibo_list_li.eq(current_weibo_index).trigger("click");
                break;
                
            //key:w, close message
            case '119':
                $('#weibo_itme_close').trigger("click");
                break;
            
        } 
    };
    function close_msg_box()
    {
        //msg_box.overlay().close();
        $('#weibo_loading_con').hide(); 
    }
    
    function weibo_scroll()
    {
        var position = weibo_list_li.eq(current_weibo_index).offset();
        window.scrollTo(0,position.top);
        weibo_focus();
    }
    
    function weibo_focus()
    {
        weibo_list_li.removeClass('weibo_list_focused');
        weibo_list_li.eq(current_weibo_index).addClass('weibo_list_focused');
    }
    
    function insertItem(data)
    {
        if(data.result){
            $('#weibo_item').html(data.message);
        }
    }
    
    function get_loading_white_img()
    {
        var code = '<div id="ajax_loader"><img src="/images/ajax-loader-white.gif" alt="loading" /></div>';
        return code;
    }
    function get_loading_img()
    {
        var code = '<div id="ajax_loader"><img src="/images/ajax-loader.gif" alt="loading" /></div>';
        return code;
    }
    
    function insertWeibo()
    {
        current_weibo = $(this);
        current_weibo_index = $('.weibo_list li').index(current_weibo);
        $('#weibo_item').css('height', $(window).height() - 55);
        weibo_focus();
        
        if(weibo_id == current_weibo.attr('id'))
        {
            return false;
        }
        else
        {
            weibo_id = current_weibo.attr('id');
            
            var position = current_weibo.offset();
            window.scrollTo(0,position.top);
            
            $('#weibo_item').html(get_loading_img());

            $.ajax({
                dataType : 'json',
                type : 'GET',
                url : '/weibo/item/'+weibo_id,
                success : insertItem
            });

            $('#weibo_items').hide("fast");
            $('#weibo_items').show("fast");

        }
    }
    
    function closeWeiboItem()
    {
        $('#weibo_items').hide("fast");
        weibo_id = '';

        return false;
    }
    
    $(function(){
        weibo_list_li = $('.weibo_list li');
        
        msg_box = $("#msg_box").overlay({
            top: '50%',
                left: '10%',
            mask: {
                color: '#fff',
                loadSpeed: 200,
                opacity: 0.5
            },
            closeOnClick: true
        });
        
        $('#weibo_get_new_msg_button').live('click',function(){
            $.ajax({
                dataType : 'json',
                type : 'GET',
                url : '/weibo/sync',
                success : function (data){
                    if(data.result){
                        $(data.message).
                            find('li').click(insertWeibo).end().
                            find('#weibo_itme_close').click(closeWeiboItem).end().
                            prependTo($('.weibo_list'));
                        
                        weibo_list_li = $('.weibo_list li');
                        current_weibo_index = 0;
                        weibo_scroll();
                    }

                    $('#weibo_loading_con').html('Get new message : '+data.count );
                    $('#weibo_sync_msg').html('Get new message : '+data.count );
                    
                    setTimeout(close_msg_box,1000);
                    
                    return false;
                }
            });
        });

        weibo_list_li.click(insertWeibo);
        $('#weibo_itme_close').click(closeWeiboItem);
    });
    
</script>

<?php echo $this->load->view('footer'); ?>
